package com.resys.service.impl;

import java.io.File;
import java.io.FileInputStream;
import java.io.IOException;
import java.io.InputStream;
import java.math.BigDecimal;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.List;


import javax.annotation.Resource;
import javax.servlet.ServletOutputStream;
import javax.servlet.http.HttpServletResponse;

import com.resys.entity.vo.SalesReportVo;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;
import org.springframework.stereotype.Service;

import com.resys.entity.enums.PageSize;
import com.resys.entity.query.ReservationorderQuery;
import com.resys.entity.po.Reservationorder;
import com.resys.entity.vo.PaginationResultVO;
import com.resys.entity.query.SimplePage;
import com.resys.mappers.ReservationorderMapper;
import com.resys.service.ReservationorderService;
import com.resys.utils.StringTools;


/**
 *  业务接口实现
 */
@Service("reservationorderService")
public class ReservationorderServiceImpl implements ReservationorderService {

	@Resource
	private ReservationorderMapper<Reservationorder, ReservationorderQuery> reservationorderMapper;
	private final String PATH = System.getProperty("user.dir") + "/static/template/";
	/**
	 * 根据条件查询列表
	 */
	@Override
	public List<Reservationorder> findListByParam(ReservationorderQuery param) {
		return this.reservationorderMapper.selectList(param);
	}

	/**
	 * 根据条件查询列表
	 */
	@Override
	public Integer findCountByParam(ReservationorderQuery param) {
		return this.reservationorderMapper.selectCount(param);
	}

	/**
	 * 分页查询方法
	 */
	@Override
	public PaginationResultVO<Reservationorder> findListByPage(ReservationorderQuery param) {
		int count = this.findCountByParam(param);
		int pageSize = param.getPageSize() == null ? PageSize.SIZE15.getSize() : param.getPageSize();

		SimplePage page = new SimplePage(param.getPageNo(), count, pageSize);
		param.setSimplePage(page);
		List<Reservationorder> list = this.findListByParam(param);
		PaginationResultVO<Reservationorder> result = new PaginationResultVO(count, page.getPageSize(), page.getPageNo(), page.getPageTotal(), list);
		return result;
	}

	/**
	 * 新增
	 */
	@Override
	public Integer add(Reservationorder bean) {
		return this.reservationorderMapper.insert(bean);
	}

	/**
	 * 批量新增
	 */
	@Override
	public Integer addBatch(List<Reservationorder> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.reservationorderMapper.insertBatch(listBean);
	}

	/**
	 * 批量新增或者修改
	 */
	@Override
	public Integer addOrUpdateBatch(List<Reservationorder> listBean) {
		if (listBean == null || listBean.isEmpty()) {
			return 0;
		}
		return this.reservationorderMapper.insertOrUpdateBatch(listBean);
	}

	/**
	 * 多条件更新
	 */
	@Override
	public Integer updateByParam(Reservationorder bean, ReservationorderQuery param) {
		StringTools.checkParam(param);
		return this.reservationorderMapper.updateByParam(bean, param);
	}

	/**
	 * 多条件删除
	 */
	@Override
	public Integer deleteByParam(ReservationorderQuery param) {
		StringTools.checkParam(param);
		return this.reservationorderMapper.deleteByParam(param);
	}

	/**
	 * 根据OrderId获取对象
	 */
	@Override
	public Reservationorder getReservationorderByOrderId(Integer orderId) {
		return this.reservationorderMapper.selectByOrderId(orderId);
	}

	/**
	 * 根据OrderId修改
	 */
	@Override
	public Integer updateReservationorderByOrderId(Reservationorder bean, Integer orderId) {
		return this.reservationorderMapper.updateByOrderId(bean, orderId);
	}

	/**
	 * 根据OrderId删除
	 */
	@Override
	public Integer deleteReservationorderByOrderId(Integer orderId) {
		return this.reservationorderMapper.deleteByOrderId(orderId);
	}

	@Override
	public void addOrder(Reservationorder reservationorder) {
		this.reservationorderMapper.add(reservationorder);
	}

	@Override
	public BigDecimal getSales(LocalDateTime start, LocalDateTime end) {

		return this.reservationorderMapper.getSales(start,end);
	}

	@Override
	public Double getRate(LocalDateTime start, LocalDateTime end) {
		Integer cancell = this.reservationorderMapper.getCancell(start,end);
		Integer all = this.reservationorderMapper.getAll(start,end);
		if(all==0) {
			return 0.0;
		}
		return (double) (all-cancell)/all;
	}

	@Override
	public void export(HttpServletResponse response) {
		LocalDate begin = LocalDate.now().minusDays(30);
		LocalDate end = LocalDate.now().minusDays(1);
        LocalDateTime beginTime = LocalDateTime.of(begin, LocalTime.MIN);
		LocalDateTime endTime = LocalDateTime.of(end, LocalTime.MIN);
		SalesReportVo vo = new SalesReportVo();
		vo.setSales(this.reservationorderMapper.getSales(beginTime,endTime));
        vo.setCompleteRate(this.getRate(beginTime,endTime));

		try {
			FileInputStream in = new FileInputStream(new File(PATH+"会议室预定系统数据报表模板.xlsx"));
			XSSFWorkbook excel = new XSSFWorkbook(in);
			XSSFSheet sheet = excel.getSheet("Sheet");
			if (sheet == null) {
				throw new IllegalArgumentException("Sheet 'Sheet' 不存在.");
			}
			sheet.getRow(1).getCell(1).setCellValue("时间"+begin+"-"+end);
			sheet.getRow(3).getCell(2).setCellValue(vo.getSales().doubleValue());
			sheet.getRow(3).getCell(4).setCellValue(vo.getCompleteRate());
			sheet.getRow(3).getCell(6).setCellValue(this.reservationorderMapper.getAll(beginTime,endTime)-this.reservationorderMapper.getCancell(beginTime,endTime));
			for (int i = 0; i < 30; i++) {
				LocalDate date = begin.plusDays(i);
				SalesReportVo salesReportVo = new SalesReportVo();
				BigDecimal sales= this.reservationorderMapper.getSales(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX));
				if(sales == null) {
					sales = BigDecimal.ZERO;
				}
				salesReportVo.setSales(sales);
				salesReportVo.setCompleteRate(this.getRate(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX)));
				sheet.getRow(6+i).getCell(1).setCellValue(date.toString());
				sheet.getRow(6+i).getCell(2).setCellValue(salesReportVo.getSales().doubleValue());
				sheet.getRow(6+i).getCell(4).setCellValue(salesReportVo.getCompleteRate());
				sheet.getRow(6+i).getCell(6).setCellValue(this.reservationorderMapper.getAll(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX))-this.reservationorderMapper.getCancell(LocalDateTime.of(date, LocalTime.MIN), LocalDateTime.of(date, LocalTime.MAX)));

			}
			ServletOutputStream out =  response.getOutputStream();
			excel.write(out);
			excel.close();
			out.close();
		} catch (IOException e) {
			e.printStackTrace();
		}



	}
}